package com.socialuni.center.web.user.login;

import com.socialuni.center.web.model.RO.CenterLoginRO;
import com.socialuni.center.web.model.RO.user.CenterMineUserDetailRO;
import com.socialuni.sdk.constant.ProviderLoginType;
import com.socialuni.sdk.constant.platform.PlatformType;
import com.socialuni.sdk.model.BindPhoneVO;
import com.socialuni.sdk.model.ProviderLoginQO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service
@Slf4j
public class CenterLoginService {
    @Resource
    CenterPhoneLoginDomain centerPhoneLoginDomain;
    @Resource
    CenterProviderLoginDomain centerProviderLoginDomain;

    //旧版代码兼容
    private void oldVersionCompatibility(ProviderLoginQO loginQO) {
        //Provider = 'weixin' | 'qq'
        String provider = loginQO.getProvider();
        //手机登陆的时候为ProviderType.phone
        if (StringUtils.isEmpty(provider)) {
            provider = loginQO.getLoginType();
            loginQO.setProvider(provider);
        }
        String platform = loginQO.getPlatform();
        if (PlatformType.android.equals(platform)) {
            loginQO.setPlatform(PlatformType.app);
        }
    }

    //因旧版本原因合在了一起，新版本要拆开
    @Transactional
    public CenterLoginRO<CenterMineUserDetailRO> providerAndPhoneLogin(ProviderLoginQO loginQO, BindPhoneVO bindPhoneVO) {
        //todo 这接口有问题，应该拆开，手机号登陆不应该和三方登陆在一起
        if (loginQO != null) {
            this.oldVersionCompatibility(loginQO);
            String provider = loginQO.getProvider();
            if (ProviderLoginType.values.contains(provider)) {
                return centerProviderLoginDomain.providerLogin(loginQO);
            }
        }
        return centerPhoneLoginDomain.phoneLogin(bindPhoneVO);
    }
}
